package com.bw.ppjoke;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.bw.ijk.widget.IjkView;
import com.bw.ijk.widget.media.IjkVideoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class MainActivity extends AppCompatActivity {

    private RecyclerView main_rv;
    private List<HashMap<String,String>>datas = new ArrayList<>();
    private MainAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initData();
        adapter.setNewInstance(datas);
//        findViewById(R.id.start_btn).setOnClickListener(v->{
//            startActivity(new Intent(MainActivity.this,TestActivity.class));
//        });
//        IjkMediaPlayer.loadLibrariesOnce(null);
//        IjkMediaPlayer.native_profileBegin("libijkplayer.so");
//        ijkVideoView = findViewById(R.id.ijk_player);
//        ijkVideoView.setPlayUrl("http://118.195.161.134:8066/fileDownload?fileName=orange.mp4");
//        ijkVideoView.start();
    }

    private void init(){
        main_rv = findViewById(R.id.main_rv);
        adapter = new MainAdapter();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        main_rv.setLayoutManager(manager);
        main_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE){
                    //停止状态->加载当前页中顶部显示的视频->获取顶部显示的item进行播放
                    LinearLayoutManager recylerManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int firstPosittion = recylerManager.findFirstCompletelyVisibleItemPosition();
                    //显示当前item
                    View view = recylerManager.findViewByPosition(firstPosittion);
                    if (view!=null){
                        IjkView ijkView = view.findViewById(R.id.main_item_ijk);
                        String url = datas.get(firstPosittion).get("url");
//                        if (!ijkView.isPlaying())
//                        if (ijkView.isPlaying())
//                            return;
                        Log.e("ZXY","停止滑动开始播放");
                        ijkView.setPlayUrl(url);
                    }

                }else if (newState == RecyclerView.SCROLL_STATE_SETTLING||newState == RecyclerView.SCROLL_STATE_DRAGGING){
                    Log.e("ZXY","SCROLL_STATE_SETTLING");
                    //手指抛动->recyclerview惯性滑动动作
                    //全部正在播放的item->停止播放
                    LinearLayoutManager recylerManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    for (int i = 0;i < recyclerView.getAdapter().getItemCount();i++){
                        View view = recylerManager.findViewByPosition(i);
                        if (view!=null){
                            IjkView ijkView = view.findViewById(R.id.main_item_ijk);
                            if (ijkView.isPlaying())
                                ijkView.stop();
                        }
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        main_rv.setAdapter(adapter);
    }

    private void initData(){
        for (int i = 0; i< 10;i ++){
            HashMap<String,String>map=new HashMap<>();
            map.put("url","http://118.195.161.134:8066/fileDownload?fileName=orange.mp4");
            map.put("imgUrl","https://img11.360buyimg.com/n7/jfs/t2968/143/2485546147/238650/70df281e/57b12a31N8f4f75a3.jpg");
            datas.add(map);
        }
    }


}